package com.sample.springbatch.job.demo.config;

import com.sample.springbatch.job.config.BaseConfig;
import com.sample.springbatch.job.demo.service.BatchDemoService;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by zwg on 2017/9/13.
 */
@Configuration
public class BatchDemoConfig extends BaseConfig {

    @Autowired
    BatchDemoService batchDemoService;



    ////////////////////////////////////////////////////////////////////////////////
    @Bean(name = "demoStartJob")
    public Job demoStartJob() {
        return jobBuilderFactory.get("demoStartJob")
                .incrementer(new RunIdIncrementer())
                .listener(myJobListener)
                .start(startCheckStep())
                .next(initDataStep())
                .build();
    }

    @Bean(name = "startCheckStep")
    public Step startCheckStep(){
        return stepBuilderFactory.get("startCheckStep")
                .tasklet(startCheckTasklet())
                .build();
    }


    @Bean(name = "startCheckTasklet")
    public Tasklet startCheckTasklet(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(batchDemoService);
        tasklet.setTargetMethod("startCheck");
        return tasklet;
    }

    @Bean(name = "initDataTasklet")
    public Tasklet initDataTasklet(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(batchDemoService);
        tasklet.setTargetMethod("initData");
        return tasklet;
    }

    @Bean(name="initDataStep")
    public Step initDataStep(){
        return stepBuilderFactory.get("initDataStep")
                .tasklet(initDataTasklet())
                .build();
    }

    /////////////////////////////////////////////////////////////////////////////////////////
    @Bean(name = "demoEndJob")
    public Job demoEndJob() {
        return jobBuilderFactory.get("demoEndJob")
                .incrementer(new RunIdIncrementer())
                .listener(myJobListener)
                .start(batchEndStep())
                .build();
    }

    @Bean(name="batchEndStep")
    public Step batchEndStep(){
        return stepBuilderFactory.get("batchEndStep")
                .tasklet(initDataTasklet())
                .build();
    }

    @Bean(name = "batchEndTasklet")
    public Tasklet batchEndTasklet(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(batchDemoService);
        tasklet.setTargetMethod("batchEnd");
        return tasklet;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////
}
